package com.china08.yxyapi.util;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

import com.squareup.pollexor.Thumbor;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;

/**
 * 图片在线处理工具类
 * 
 * @author Ertao.Fang
 * 
 */
@Component
public class ThumborUtil {

    private static Thumbor thumbor;

    private static final String HOST = "aHR0cDovL3RodW1ib3IuOTF5dW54aWFvLmNvbQ==";

    private static final String KEY = "NWY3MmJhZjExMGIwZjU5MTlkOWRjNzgyN2ZmYmI5ZjI=";

    public static Thumbor getThumborInstance() {
        if (thumbor == null) {
            thumbor = Thumbor.create(new String(Base64.decodeBase64(HOST)), new String(Base64.decodeBase64(KEY)));
        }
        return thumbor;
    }

    /**
     * 改变图片大小
     * 
     * @param imgUrl
     * @param w
     * @param h
     * @return
     */
    public static String resize(String imgUrl, Integer w, Integer h) {
        // 判断图片是否缺少http
        if (!imgUrl.contains("http")) {
            String bb = "http:";
            imgUrl = bb + imgUrl;
        }
        String newImgUrl = imgUrl;
        // 内网地址
        if (imgUrl != null && imgUrl.indexOf("192.168.") != -1) { return imgUrl; }
        // 外网地址
        try {
            ThumborUrlBuilder tub = ThumborUtil.getThumborInstance().buildImage(imgUrl);
            // tub.align(VerticalAlign.TOP);
            tub.resize(w, h);
            tub.smart();
            tub.trim();
            tub.filter(ThumborUrlBuilder.quality(100));
            tub.filter(ThumborUrlBuilder.format(ImageFormat.JPEG));
            // tub.filter(ThumborUrlBuilder.watermark(
            // "https://www.91yunxiao.com/resources/images/logo.png",
            // w - 200, h - 100, 30));
            newImgUrl = tub.toUrl();
        } catch (Exception e) {
            newImgUrl = imgUrl;
        }
        return newImgUrl;

    }

    /**
     * 学校网站--改变图片大小
     * 
     * @param imgUrl
     * @param w
     * @param h
     * @return
     */
    public static String schoolWebSiteResize(String imgUrl, Integer w, Integer h) {
        String newImgUrl = "";
        if (imgUrl.length() > 0) {
            newImgUrl = imgUrl;
            // 判断图片是否缺少http
            if (!imgUrl.contains("http")) {
                String bb = "http:";
                imgUrl = bb + imgUrl;
            }
            // 内网地址
            if (imgUrl != null && imgUrl.indexOf("192.168.") != -1) { return imgUrl; }
            // 外网地址
            try {
                ThumborUrlBuilder tub = ThumborUtil.getThumborInstance().buildImage(imgUrl);
                // tub.align(VerticalAlign.TOP);
                tub.resize(w, h);
                tub.smart();
                // tub.trim();
                tub.filter(ThumborUrlBuilder.quality(100));
                if (imgUrl.endsWith("jpg") || imgUrl.endsWith("jpeg") || imgUrl.endsWith("JPG")
                        || imgUrl.endsWith("JPEG")) {
                    tub.filter(ThumborUrlBuilder.format(ImageFormat.JPEG));
                } else if (imgUrl.endsWith("PNG") || imgUrl.endsWith("png")) {
                    tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                } else if (imgUrl.endsWith("bmp") || imgUrl.endsWith("BMP")) {
                    tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                } else if (imgUrl.endsWith("gif") || imgUrl.endsWith("GIF")) {
                    tub.filter(ThumborUrlBuilder.format(ImageFormat.GIF));
                }
                // tub.filter(ThumborUrlBuilder.watermark(
                // "https://www.91yunxiao.com/resources/images/logo.png",
                // w - 200, h - 100, 30));

                newImgUrl = tub.toUrl();
                // if (newImgUrl.startsWith("https")) {
                // newImgUrl = newImgUrl.substring(6, newImgUrl.length());
                // } else {
                // newImgUrl = newImgUrl.substring(5, newImgUrl.length());
                // }
                if (!newImgUrl.startsWith("https")) {
                    newImgUrl = "https:" + newImgUrl.substring(5, newImgUrl.length());
                }
            } catch (Exception e) {
                newImgUrl = imgUrl;
            }
        }
        return newImgUrl;

    }

}
